// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "Character/Q3D_CPPA_Character.h"
#include "PawnState/Q3D_CPPC_PawnState.h"
#include "Property/Q3D_CPPC_PropertyState.h"
#include "Property/Q3D_CPPE_PropertyName.h"
#include "Engine/DataTable.h"
#include "Logger/Q3D_CPPU_Logger.h"

// Sets default values
AQ3D_CPPA_Character::AQ3D_CPPA_Character()
{
	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	this->Ptr_C_PawnState = CreateDefaultSubobject<UQ3D_CPPC_PawnState>(TEXT("UQ3D_CPPC_PawnState"));
	this->Ptr_C_PropertyState = CreateDefaultSubobject<UQ3D_CPPC_PropertyState>(TEXT("UQ3D_CPPC_PropertyState"));
}

// Called when the game starts or when spawned
void AQ3D_CPPA_Character::BeginPlay()
{
	Super::BeginPlay();

	// 往PropertyState中写入初始数据
	UDataTable *ptr_dt_property_init_tmp;
	this->Q3D_IF_PropertyState_InitDT_Get_Implementation(ptr_dt_property_init_tmp);
	this->Ptr_C_PropertyState->Q3D_F_Model_Set(ptr_dt_property_init_tmp);
}

// Called every frame
void AQ3D_CPPA_Character::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void AQ3D_CPPA_Character::SetupPlayerInputComponent(UInputComponent *PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void AQ3D_CPPA_Character::PossessedBy(AController *NewController)
{
	Super::PossessedBy(NewController);
	Q3D_LOG(Log, "[" + NewController->GetName() + "]");
	this->Ptr_C_PawnState->Q3D_F_Pawn_Possessed(NewController);
}

void AQ3D_CPPA_Character::UnPossessed()
{
	AController *const OldController = Controller;
	Super::UnPossessed();
	Q3D_LOG(Log, "From_[" + OldController->GetName() + "]");
	this->Ptr_C_PawnState->Q3D_F_Pawn_Unpossessed(OldController);
}

void AQ3D_CPPA_Character::Q3D_IF_PlayerIndex_Get_Implementation(int32 &int_player_index) const
{
	int_player_index = this->Int_PlayerIndex;
	return;
}

void AQ3D_CPPA_Character::Q3D_IF_PawnState_Get_Implementation(UQ3D_CPPU_PawnState *&ptr_u_pawn_state) const
{
	ptr_u_pawn_state = this->Ptr_C_PawnState->Ptr_U_PawnState;
	return;
}

void AQ3D_CPPA_Character::Q3D_IF_PropertyState_Get_Implementation(UQ3D_CPPU_PropertyState *&ptr_u_property_state) const
{
	// if( ! this->Ptr_C_PropertyState ){
	// 	Q3D_LOG( Warning, "Ptr_C_PropertyState_Not_Ready" );
	// 	ptr_u_property_state = nullptr;
	// 	return;
	// }
	ptr_u_property_state = this->Ptr_C_PropertyState->Ptr_U_PropertyState;
	return;
}

void AQ3D_CPPA_Character::Q3D_IF_PropertyState_InitDT_Get_Implementation(UDataTable *&ptr_dt_property_init) const
{
	// Owner 已经从外部对 Ptr_DT_PropertyInit 进行了赋值，优先级最高，直接使用
	if (this->Ptr_DT_PropertyInit)
	{
		ptr_dt_property_init = this->Ptr_DT_PropertyInit;
		Q3D_LOG(Log, "DT_Load_From_Ptr_DT_PropertyInit");
		return;
	}

	// 子类未赋值DT，则属性列表为空
	Q3D_LOG(Log, "DT_Load_Nothing");
	return;
}
